package org.steven.crm.core.service.systemuser;

import org.steven.crm.core.service.BaseService;
import com.steven.sequence.generater.SnowflakeGenerator;
import com.steven.xdb.dbmanager.DBManager;
import com.steven.xdb.pagination.query.AbstractQueryDataList;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author wuzw
 * @Date 2023/12/25 14:18
 * @Version 1.0
 */
public class SystemUserRoleService extends BaseService {
    @Override
    protected String getTable() {
        return "systemUserRole";
    }

    @Override
    protected AbstractQueryDataList getQueryDataList() {
        return null;
    }

    @Override
    protected String getDataSourceName() {
        return null;
    }

    public List<Map<String, String>> getSystemUserRoles(String systemUserId) throws Exception {
        Map<String, String> k = new HashMap<>();
        k.put("systemUserId", systemUserId);
        return DBManager.query(getSession(), getTable(), k);
    }

    public List<String> getSystemUserRoleIds(String systemUserId) throws Exception {
        List<Map<String, String>> roles = getSystemUserRoles(systemUserId);
        return roles.stream().map(e -> e.get("systemRoleId")).collect(Collectors.toList());
    }

    public void batchSave(String systemUserId, Collection<String> newSystemRoleIds) throws Exception {
        List<String> curRoleIds = getSystemUserRoleIds(systemUserId);

        Collection<String> keepRoleIds = CollectionUtils.intersection(newSystemRoleIds, curRoleIds);
        Collection<String> deleteRoleIds = CollectionUtils.subtract(curRoleIds, keepRoleIds);
        Collection<String> addRoleIds = CollectionUtils.subtract(newSystemRoleIds, keepRoleIds);

        if (deleteRoleIds.size() > 0) {
            String sql = "delete from " + getTable() + " where systemUserId = ? and systemRoleId in ("
                    + deleteRoleIds.stream().collect(Collectors.joining(",")) + ")";
            List<String> p = new ArrayList<>();
            p.add(systemUserId);
            DBManager.update(getSession(), sql, p);
        }

        if (addRoleIds.size() > 0) {
            List<Map<String, String>> addDatas = new ArrayList<>();
            for (String addRoleId : addRoleIds) {
                Map<String, String> addData = new HashMap<>();
                addData.put("id", SnowflakeGenerator.getIndex());
                addData.put("systemUserId", systemUserId);
                addData.put("systemRoleId", addRoleId);
                addDatas.add(addData);
            }
            DBManager.batchInsert(getSession(), getTable(), addDatas);
        }
    }

    public Map<String, List<String>> batchGetRelateRoleIds(List<String> systemUserIds) throws Exception {
        if (systemUserIds == null || systemUserIds.size() == 0) {
            return new HashMap<>();
        }

        String sql = "select * from " + getTable() + " where systemUserId in (" + String.join(",", systemUserIds) + ")";
        List<String> p = new ArrayList<>();
        List<Map<String, String>> datas = DBManager.query(getSession(), sql, p);

        Map<String, List<String>> result = new HashMap<>();
        for (Map<String, String> data : datas) {
            String systemUserId = data.get("systemUserId");
            if (!result.containsKey(systemUserId)) {
                result.put(systemUserId, new ArrayList<>());
            }

            result.get(systemUserId).add(data.get("systemRoleId"));
        }

        return result;
    }
}
